package singleton_pattern;

/**
 * 双重检查锁，解决多线程可能出现的多实例问题
 */
public class SingleTon2 {

    private static volatile SingleTon2 singleton;

    private SingleTon2() {
    }

    public static SingleTon2 getInstance() {
        if (singleton == null) {
            synchronized (SingleTon2.class) {
                if (singleton == null) {
                    singleton = new SingleTon2();
                }
            }
        }
        return singleton;
    }

    //类中其他方法，尽量是static
    public static void doSomething() {
        System.out.println("this is a singleInstance");
    }
}
